<?php

class Main_user extends Controller {

	function Main_user() {
		parent::Controller();	
        $this->load->model('w_user_mod', 'u');              
	}
	
 
    
	function index() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
            
            $this->list_user();
        } 
	}
    

    //user
    function list_user() {
    	if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        
        $data['additional_js'] = '
        <script type="text/javascript">
         function deleteRow(id) {  
        var txt = \'Anda yakin mau menghapus data ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
                url = "'.base_url().'main_user/hapus_user/"+id;
             $.ajax({
               type: "POST",
               url: "'.base_url().'main_user/user_bisa_dihapus/"+id,
               success: function(msg) {
                 if (msg==\'bisa\') {
                    $.prompt(txt,{
                        buttons:{Hapus:true, Batal:false},
                        callback: function(v){
                            if(v) {
                                $("tr#row-"+id).fadeOut("slow", function() {
                                    $("tr#row-"+id).remove();
                                });
                                top.location.href=url;
                            }
                        }
                    });
                 } else {
                    alert(\'Maaf, data user tidak dapat dihapus dikarenakan sedang digunakan oleh data lain.\');
                 }
               }
            });    
            } </script>    
            ';
     
          
        $data['list_data'] = $this->u->list_user();
        $data['judul'] = 'LIST DATA USER';
        $data['tampilkan_menu'] = true; 
        $data['aksi'] = 'main_user/tambah_user';    
        $this->load->view('v_user',$data); 
    }
    
    function tambah_user() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        
        $data['additional_js'] = '
        <script type="text/javascript">
            $(function() {
            $("#namauser").focus();
            $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'main_user\';
                });
            // validate contact form on keyup and submit
            $("#tambahuser").validate({
                //set the rules for the field names
                rules: {
                    namauser: { required: true },
                    nama_lengkap: { required: true },
                    passwd: { required: true },
                    passwd2: { required: true },
                    jenis_admin: { required: true }
                },
                //set messages to appear inline
                messages: {
                    namauser: "Nama user wajib diisi",
                    nama_lengkap: "Nama lengkap wajib diisi",
                    passwd: "Password wajib diisi",
                    passwd2: "Password wajib diisi",
                    jenis_admin: "Grup user wajib diisi"
                }
            });

            $("form").submit(function() {
                if ($("#passwd").val() != $("#passwd2").val()) {
                    alert(\'Password harus sama!!!\');
                    return false;
                }
            });
        });
        </script>
';

        $data['additional_css'] = '
            <style type="text/css">
            .error {
                color: red;
                font: 8pt verdana;
                padding-left: 10px
            }
            </style>
        ';
        
        $data['list_data'] = $this->u->entry_user('simpan_user');
            
        $data['url_list'] = base_url().'main_user/list_user'; 
        $data['judul'] = 'TAMBAH USER';
       
        $this->load->view('v_user',$data); 
        
    }
    
    

    function simpan_user() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        

        $namauser = $this->input->post('namauser');
        $nama_lengkap = $this->input->post('nama_lengkap');
        $passwd = md5($this->input->post('passwd'));
        $email = $this->input->post('email');
        $id_grup = $this->input->post('jenis_admin');
        
       
        $this->u->simpan_user($namauser,$nama_lengkap,$passwd,$email,$id_grup);                 
        
        redirect('main_user','refresh');    
         
    }
    
    function edit_user($namauser) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        
        $data['additional_js'] = '
        <script type="text/javascript">
            $(function() {
            $("#namauser").focus();
            $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'main_user\';
                });
            // validate contact form on keyup and submit
            $("#tambahuser").validate({
                //set the rules for the field names
                rules: {
                    namauser: { required: true },
                    nama_lengkap: { required: true },
                    jenis_admin: { required: true }
                },
                //set messages to appear inline
                messages: {
                    namauser: "Nama user wajib diisi",
                    nama_lengkap: "Nama lengkap wajib diisi",
                    jenis_admin: "Grup user wajib diisi"
                }
            });

            $("form").submit(function() {
                if ($("#passwd").val() != $("#passwd2").val()) {
                    alert(\'Password harus sama!!!\');
                    return false;
                }
            });
        });
        </script>
';

        $this->db->where(array('namauser' => $namauser));
        $q = $this->db->get('user');
        if ($q->num_rows() > 0) {
            $user = $q->row();
            $namauser        = $user->namauser;
            $nama_lengkap    = $user->nama_asli;
            $email           = $user->email;
            $id_grup         = $user->pta_grup_id;
        }
        
        $data['list_data'] = $this->u->entry_user('update_user',$namauser,$nama_lengkap,'',$email,$id_grup);
        $data['judul'] = 'EDIT USER';
            
        $this->load->view('v_user',$data); 
       
    }
    
    function update_user() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
     
        $namauser = $this->input->post('namauser');
        $nama_lengkap = $this->input->post('nama_lengkap');
        $email = $this->input->post('email');
        if ($this->input->post('passwd')) {
            $passwd = md5($this->input->post('passwd'));
        } else {
            $passwd = '';
        }
        $id_grup = $this->input->post('jenis_admin');
        
        $this->u->simpan_user($namauser,$nama_lengkap,$passwd,$email,$id_grup,TRUE);                 
        
        redirect('main_user','location');      
    }
    
    function hapus_user($namauser) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }
        
        $this->db->where('namauser', $namauser);
        $this->db->delete('user');
        redirect('main_user','refresh');
    }
    
    //grup    
    function tambah_grup() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        
         $data['additional_js'] = '
        <script type="text/javascript">
            $(function() {
            
            $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'main_user/list_grup\';
                });
            // validate contact form on keyup and submit
            $("#grup").validate({
                //set the rules for the field names
                rules: {
                    nama: { required: true },
                    
                   
                },
                //set messages to appear inline
                messages: {
                    nama: "Nama Grup wajib diisi",
                    
                    
                }
            });

      
        });
        </script>
        ';
        
        $action = base_url().'main_user/simpan_grup';
        $data['list_data'] = $this->u->entry_grup($action); 
        $data['judul'] = 'TAMBAH GRUP';
        $data['url_list'] = base_url().'main_user/list_grup';                
        $this->load->view('v_user',$data);    

       
    }
    
     //grup
     function list_grup() {
       if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }  
        
        
        $data['additional_js'] = '
        <script type="text/javascript">        
        function deleteRow(id) {
            var txt = \'Anda yakin mau menghapus data ini?<input type="hidden" id="row-id" name="row-id" value="\'+ id +\'" />\';
                url = "'.base_url().'main_user/hapus_grup/"+id;
             $.ajax({
               type: "POST",
               url: "'.base_url().'main_user/grup_bisa_dihapus/"+id,
               success: function(msg) {
                 if (msg==\'bisa\') {
                    $.prompt(txt,{
                        buttons:{Hapus:true, Batal:false},
                        callback: function(v){
                            if(v) {
                                $("tr#row-"+id).fadeOut("slow", function() {
                                    $("tr#row-"+id).remove();
                                });
                                top.location.href=url;
                            }
                        }
                    });
                 } else {
                    alert(\'Maaf, data grup tidak dapat dihapus dikarenakan sedang digunakan oleh data lain.\');
                 }
               }
            });
        }
         </script> 
        ';

        $data['list_data'] = $this->u->list_grup('', 0, 0, true);
        $data['judul'] = 'LIST DATA GRUP';
        $data['tampilkan_menu'] = true; 
        $data['aksi'] = 'main_user/tambah_grup'; 
        $data['ini_grup'] = true;
        $this->load->view('v_user',$data); 
        //echo  $this->l->list_grup();
       
    }
    
    function simpan_grup() {
       if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }   
        
        $nama = $this->input->post('nama');
        
        $this->u->simpan_grup('grup', $nama);                    
    
        redirect('main_user/list_grup','refresh');      
    
    }
    
    
    function edit_grup($id) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }   
        
        $data['additional_js'] = '
        <script type="text/javascript">
            $(function() {
            
            $("#btn_kembali").click( function() {
                    window.location = \''.base_url().'main_user/list_grup\';
                });
            // validate contact form on keyup and submit
            $("#grup").validate({
                //set the rules for the field names
                rules: {
                    nama: { required: true },
                    
                   
                },
                //set messages to appear inline
                messages: {
                    nama: "Nama Grup wajib diisi",
                    
                    
                }
            });

      
        });
        </script>
        ';
        
        $this->db->where(array('idpta_grup' => $id));
        $q = $this->db->get('grup');
        if ($q->num_rows() > 0) {
            $grup       = $q->row();
            $nama       = $grup->nama;
            $id_grup    = $grup->idpta_grup;
        }
        
        
        $action = base_url().'main_user/update_grup';
        $data['list_data'] = $this->u->entry_grup($action,$nama,$id_grup);
        $data['judul'] = 'EDIT GRUP';
        $data['url_list'] = base_url().'main_user/list_grup';
        
        $this->load->view('v_user',$data); 
        
    }
    
    function update_grup() {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        }   
     
        $nama = $this->input->post('nama');
        $id = $this->input->post('id');
        $this->u->simpan_grup('grup', $nama, $id, TRUE);                 
        
       redirect('main_user/list_grup','refresh');          
    }
    
    function hapus_grup($id) {
        if (!$this->session->userdata('username')) {
            $data['isLogin'] = FALSE;
            redirect("/login", "location");
        } else {
            $data['isLogin'] = TRUE;
            $data['username'] = $this->session->userdata('username');
            $data['role'] = $this->session->userdata('role');
        } 
        
        $this->db->where('idpta_grup', $id);
        $this->db->delete('grup');
        redirect('main_user/list_grup','refresh');      
        
    }
    
    function grup_bisa_dihapus($id) {
        $this->db->where('pta_grup_id', $id);
        $q = $this->db->get('user');
        if ($q->num_rows() > 0) {
            echo 'tidak';
        } else {
            echo 'bisa';
        }
    }

    function user_bisa_dihapus($namauser) {
    	if ($namauser != $this->session->userdata('username')) {
    		echo 'bisa';
    	} else {
    		echo 'not-bisa';
    	}
    }
    
    
    
    
}

/* End of file main_admin.php */
/* Location: ./system/application/controllers/main_user.php */